class Solution1 {
    public int posIndex;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        posIndex = postorder.length-1;
        return buildTreeChilde(inorder,postorder,0,inorder.length-1);
    }

    private TreeNode buildTreeChilde(int [] inorder,int[] postorder,int inbegin,int inend) {
        if(inend<inbegin) return null;
        TreeNode root = new TreeNode(postorder[posIndex]);
        int rootIndex = FindIndexRoot(inorder,postorder[posIndex],inbegin,inend);
        posIndex--;

        root.right = buildTreeChilde(inorder,postorder,rootIndex+1,inend);
        root.left = buildTreeChilde(inorder,postorder,inbegin,rootIndex-1);

        return root;
    }

    private int FindIndexRoot(int[] inorder,int key,int inbegin,int inend) {
        for(int i=inbegin;i<=inend;i++) {
            if(inorder[i] == key) {
                return i;
            }
        }
        return -1;
    }
}